from datetime import datetime

from langchain_core.messages import AIMessage, SystemMessage, HumanMessage, ChatMessage

from langchain_core.prompts import PromptTemplate, ChatPromptTemplate, HumanMessagePromptTemplate, \
    SystemMessagePromptTemplate, MessagesPlaceholder


def make_message(role: str, content: str):
    if role == "system":
        return SystemMessage(content=content)
    elif role == "assistant":
        return AIMessage(content=content)
    elif role == "user":
        return HumanMessage(content=content)
    else:
        raise ValueError(f"Invalid role: {role}")


def make_prompt(english_text: str):
    prompt = PromptTemplate.from_template("You are a helpful assistant that translates {input_language} to {output_language}.")
    prompt_value = prompt.invoke({"input_language": "English", "output_language": "Chinese"})
    return prompt_value

def make_template(text: str):
    system_message_prompt = SystemMessagePromptTemplate.from_template("You are a helpful assistant that translates {input_language} to {output_language}," +
                                                                      "每次回复都返回这个时间：{now}.")
    human_message_prompt = HumanMessagePromptTemplate.from_template("{text}")
    chat_prompt = system_message_prompt + human_message_prompt
    chat_prompt_value = chat_prompt.invoke({
        "text": text,
        "input_language": "English",
        "output_language": "Chinese",
        "now": datetime.now()
    })
    # chat_prompt = ChatPromptTemplate.from_messages([
    #     system_message_prompt,
    #     human_message_prompt
    # ]).partial(now=datetime.now())
    # chat_prompt_value = chat_prompt.invoke({
    #     "text": text,
    #     "input_language": "English",
    #     "output_language": "Chinese"
    # })
    return chat_prompt_value.to_string()

def make_role(text: str):
    # Modern approach using ChatPromptTemplate with direct string composition
    template = """You are a helpful assistant that translates {input_language} to {output_language}.

User: {example_input}
Assistant: {example_output}

User: {text}"""
    
    prompt = PromptTemplate.from_template(template)
    return prompt.invoke({
        "text": text,
        "input_language": "English",
        "output_language": "Chinese",
        "example_input": "I love programming.",
        "example_output": "我非常喜欢编程。"
    }).to_string()


def make_role_base():
    template = """You are a helpful assistant that translates {input_language} to {output_language}.

User: {example_input}
Assistant: {example_output}

User: {text}"""

    return PromptTemplate.from_template(template)

def make_role_base_explain():
    template = """You are a helpful assistant that explain the message that user input.

User: wish
Assistant: “wish”常见释义如下：

### 动词词性
1. **表示希望、想要（往往较难实现或带有幻想色彩）**
    - 后接不定式作宾语，例如：I wish to see you again.（我希望能再见到你。）
    - 后接宾语从句时，从句常用虚拟语气：
        - 若表示与现在事实相反的愿望，从句谓语动词用一般过去时（be动词常用were），比如：I wish I were a bird.（我希望自己是一只鸟。）
        - 若表示与过去事实相反的愿望，从句谓语动词用“had + 过去分词”形式，例如：He wished he had studied harder for the exam.（他希望自己当时为考试能更努力学习些。）
        - 若表示对将来情况的一种愿望，从句谓语动词常用“would + 动词原形”，像：I wish it would stop snowing.（我希望雪能停住。）
2. **用于祝愿**
    - 常用结构“wish sb. sth.”，例如：Wish you success!（祝你成功！）；We wish them a pleasant journey.（我们祝他们旅途愉快。）

### 名词词性
1. **愿望、心愿**
指内心期望达成的想法，例如：Her wish came true at last.（她的愿望最终实现了。）
2. **祝愿、祝福（常用复数形式）**
如：Best wishes for your birthday!（祝你生日快乐！）；Please give my wishes to your family.（请代我向你的家人致以祝福。）

总的来说，“wish”围绕着期望、愿望以及表达美好祝愿等意思来使用，在不同语境中有着相应的表达形式和用法特点。  

User: {text}"""

    return PromptTemplate.from_template(template)


def make_role_base_english():
    template = """You are a helpful assistant that translates English to Chinese.

User: wish
Assistant: 希望

User: {text}"""
    return PromptTemplate.from_template(template)



